﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Application.Biz;
using NFine.Code;
using NFine.Domain._03_Entity.Biz;

namespace NFine.Web.Areas.MemberManage.Controllers
{
    public class MemberWithdrawalsController : ControllerBase
    {

        private UserWithdrawalsApp userWithdrawalsApp = new UserWithdrawalsApp();
        private UserPropertyApp userPropertyApp = new UserPropertyApp();
        private UserBankApp userBankApp = new UserBankApp();

        //
        // GET: /MemberManage/MemberWithdrawals/
        

        public override ActionResult Index()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }
            
            ViewData["bankList"] = userBankApp.GetUserBankInfo(memberId);
            return View();
        }

        /// <summary>
        /// 提现页面
        public ActionResult WithDraw()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }

            ViewData["bankList"] = userBankApp.GetUserBankInfo(memberId);

            return View();
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetProperty()
        {
            //TODO 获取会员信息
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }


            // 获取会员资产信息
            UserPropertyEntity userPropertyEntity = userPropertyApp.GetMemberProperty(memberId);
            var data = new
            {
                userProperty = userPropertyEntity.F_Property,
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        public ActionResult Withdrawals(decimal money,string bankId)
        {
            //TODO 获取会员信息
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }

            /*// OpenId判断
            string openId = userWithdrawalsApp.getOpenIdById(memberId);
            if (string.IsNullOrEmpty(openId))
                return Error("不存在提现凭证，请先关注公众号并登陆个人中心！");*/

            // 提现
            int result = userWithdrawalsApp.Withdrawals(money, memberId, bankId);

            if (result > 0)
                return Success("提现成功，等待审核。");
            return Error("提现失败。");

        }

        [HttpGet]
        [HandlerAjaxOnly]
        [HandlerLogin]
        public ActionResult GetListForWithdrawals(Pagination pagination, int Type, string WithdrawalsTimeStart, string WithdrawalsTimeEnd)
        {
            //TODO 获取会员信息
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }
            var data = new
            {
                rows = userWithdrawalsApp.GetListForWithdrawals(pagination, Type, WithdrawalsTimeStart, WithdrawalsTimeEnd, memberId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public override ActionResult Form()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            ViewData["uName"] = u.UserName;
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(UserBankEntity userBankEntity)
        {
            var u = OperatorProvider.Provider.GetCurrent();
            userBankEntity.F_UserId = u.UserId;
            bool flag = userBankApp.AddBank(userBankEntity);
            if (flag)
                return Success("操作成功。");
            return Error("添加银行卡异常，请重新提交！");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUserBankInfo()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null && !u.IsSystem)
            {
                memberId = u.UserId;
            }

            var data  = userBankApp.GetUserBankInfo(memberId);
            return Content(data.ToJson());
        }

    }
}
